/*------------------------------添加备注----------------------------------*/
$(document).on('click','.addDirect',function(){
	$('.messTitle').html('添加备注');
	$('.addMessageBg').show();
	$('.addMessage').show();
	$('.messContent').removeClass('pastePic').html('');
	$('.addMessSubmit').addClass('addDirectSubmit').removeClass('addPicSubmit').attr('taskId',$(this).parent().parent().find('input[name=taskId]').val());
})
/*-----------------------------添加图片-----------------------------*/
$(document).on('click','.addPic',function(){
	$('.messTitle').html('粘贴图片');
	$('.messContent').addClass('pastePic').html('');
	$('.addMessageBg').show();
	$('.addMessage').show();
	$('.addMessSubmit').removeClass('addDirectSubmit').addClass('addPicSubmit').attr('taskId',$(this).parent().parent().find('input[name=taskId]').val());
})
	document.querySelector('.messContent').addEventListener('paste',function(e){
		 var cbd = e.clipboardData;
		    var ua = window.navigator.userAgent;
		    // 如果是 Safari 直接 return
		    if ( !(e.clipboardData && e.clipboardData.items) ) {
		        return ;
		    }
		    // Mac平台下Chrome49版本以下 复制Finder中的文件的Bug Hack掉
		    if(cbd.items && cbd.items.length === 2 && cbd.items[0].kind === "string" && cbd.items[1].kind === "file" &&
		        cbd.types && cbd.types.length === 2 && cbd.types[0] === "text/plain" && cbd.types[1] === "Files" &&
		        ua.match(/Macintosh/i) && Number(ua.match(/Chrome\/(\d{2})/i)[1]) < 49){
		        return;
		    }

		    for(var i = 0; i < cbd.items.length; i++) {
		        var item = cbd.items[i];
		        if(item.kind == "file"){
		            var blob = item.getAsFile();
		            if (blob.size === 0) {
		                return;
		            }
		            // blob 就是从剪切板获得的文件 可以进行上传或其他操作
					var data = new FormData();
					data.append('pic', blob);
					/*data.append('userId',1);*/
					$.ajax({
					    url: '/ps/task/addPicjson',
					    type: 'POST',
					    dataType:'json',
					    cache: false,
					    data: data,
					    processData: false,
					    contentType: false,
					    success:function(res){
					    	var wrap = document.querySelector('.messContent');
						    var file = res.data.toString();
						    var img = document.createElement("img");
					        img.src = file;
					        if(Boolean(res)){
					        	$('#upImg').val(file);
					        }
							wrap.appendChild(img);
					    },error:function(){
					    	
					    }
					})
		        }
		    }
		}, false);
function insertHtmlAtCaret(html) {
	var sel, range;
	if (window.getSelection) {
		// IE9 and non-IE
		sel = window.getSelection();
		if (sel.getRangeAt && sel.rangeCount) {
			range = sel.getRangeAt(0);
			range.deleteContents();
			// Range.createContextualFragment() would be useful here but is
			// non-standard and not supported in all browsers (IE9, for one)
			var el = document.createElement("div");
			el.innerHTML = html;
			var frag = document.createDocumentFragment(), node, lastNode;
			while ( (node = el.firstChild) ) {
				lastNode = frag.appendChild(node);
			}
			range.insertNode(frag);
			// Preserve the selection
			if (lastNode) {
				range = range.cloneRange();
				range.setStartAfter(lastNode);
				range.collapse(true);
				sel.removeAllRanges();
				sel.addRange(range);
			}
		}
	} else if (document.selection && document.selection.type != "Control") {
		// IE < 9
		document.selection.createRange().pasteHTML(html);
	}
}
$(document).on('click','.close',function(){
	//关闭弹框
	$('.addMessageBg').hide();
	$('.addMessage').hide();
})

$(document).on('click','.addDirectSubmit',function(){
	//添加备注
	var attribute = $(this).parent().find('.messContent').html();
	var psTaskId = $(this).attr('taskId');
	var installId = $('input[name=installId]').val();
	$.ajax({
	    url: '/ps/task/modifyPsTaskAction',
	    type: 'POST',
	    dataType:'json',
	    data: {
	    	'psTaskId':psTaskId,
	    	'attribute':attribute,
	    	'userId':installId
	    },
	    success:function(res){
	    	$('.addMessageBg').hide();
	    	$('.addMessage').hide();
	    },error:function(){
	    	
	    }
	})
})
$(document).on('click','.addPicSubmit',function(){
	var psTaskId = $(this).attr('taskId');
	var upImg = $('#upImg').val();
	//添加图片
	$.ajax({
	    url: '/ps/task/modifyPicAction',
	    type: 'POST',
	    dataType:'json',
	    data: {
	    	'psTaskId':psTaskId,
	    	'pic':upImg
	    },
	    success:function(res){
	    	$('.addMessageBg').hide();
	    	$('.addMessage').hide();
	    },error:function(){
	    	
	    }
	})
})

/*----------------------------绑定回车键-------------------------------*/
$(document).keydown(function(event){
	if(event.keyCode == 13){ //绑定回车 
		if($('.addMessSubmit').hasClass('addDirectSubmit')){
			$('.addDirectSubmit').click();
		}
		if($('.addMessSubmit').hasClass('addPicSubmit')){
			$('.addPicSubmit').click();
		}
		
	} 
}); 
/*----------------------------加急----------------------------------*/
$(document).on('click','.fastBtn',function(){
	$(this).html('已加急');
	$(this).addClass('fastedBtn').removeClass('fastBtn');
})
